<template>
  <el-form ref="formRef" style="max-width: 600px" :inline="true" :model="formInline" class="demo-form-inline">
    <el-form-item label="姓名：" prop="username" style=" width:250px;" :rules="[{
      required: true,
      message: '请输入姓名',
      trigger: 'blur'
    }]">
      <el-input v-model="formInline.username" placeholder="请输入姓名" clearable />
    </el-form-item>
    <el-form-item label="角色：" style=" width:250px;">
      <el-select v-model="formInline.status">
        <el-option label="普通用户" value="1" />
        <el-option label="管理员" value="2" />
      </el-select>
    </el-form-item>
    <el-form-item label="账号：" prop="loginname" style=" width:250px;" required :rules="[{
      required: true,
      message: '请输入账号',
      trigger: 'blur'
    }]">
      <el-input v-model="formInline.loginname" placeholder="请输入账号" clearable />
    </el-form-item>
    <el-form-item label="密码：" prop="password" style=" width:250px;" required :rules="[{
      required: true,
      message: '请输入密码',
      trigger: 'blur'
    }]">
      <el-input type="password" v-model="formInline.password" placeholder="请输入密码" clearable />
    </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit(formRef)">提交</el-button>
      </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { ApiPost } from '@/utils/Axios';
import { ElNotification, FormInstance } from 'element-plus'
import { reactive, ref } from 'vue'
const formRef = ref<FormInstance>()
const formInline = reactive({
  username: '',
  status: '1',
  loginname: '',
  password: ''
})

const onSubmit = (formEl:FormInstance|undefined) => {
  formEl.validate(val=>{
    if(val){
      ApiPost('/userInf/add',formInline).then(res=>{
        if(res.code === 200){
          ElNotification({
            title: '添加成功',
            type:'success',
            duration: 2000,
            showClose:false 
          })
        }else{
          ElNotification({
            title: '添加失败',
            message: res.msg,
            type: 'error',
            duration: 2000,
            showClose:false
          })
        }
      }).catch(err=>{
        console.log(err)
      })
    }
  })
}
</script>

<style></style>